<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\AdverImage;
use Symfony\Component\HttpFoundation\File\UploadedFile;

class AdverImageController extends Controller
{
    protected $model;

    public function __construct(AdverImage $adverimage)
    {
        $this->model = $adverimage;
    }
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        //
        $result = $this->model
            ->filter($request->all())
            ->orderBy('created_at', 'desc')
            ->paginate(config('api.page.limit'));
        return view('admins.adverimage.index')->with('result', $result);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create(Request $request)
    {
        
        return view('admins.adverimage.add');

    }
    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
     /*
      * 添加
      */
      public function store(Request $request)
      {
          
        $adverName = $request->adverName;
        $adverImage = $request->adverImage;
        $time = date('Y-m-d H:i:s', time());
        $data = array(
            'adver_image' => $adverImage,
            'adver_name' => $adverName,
            'created_at' => $time,
            'updated_at' => $time
        );
        $result = 0;
        $isFound = $this->model->where('adver_name',$adverName)->first();
        $msg = '该名称已存在，请勿重复添加！';
        if(!$isFound){
            $result = $this->model->insertGetId($data);
            $msg = '添加失败，请重试！';
            if ($result) {
                $msg = '添加成功！';
            }
        }
        return response()->json(['flag' => $result, 'msg' => $msg]);
      }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
  
        $result = [];
        if ($id) {
            $result = $this->model
                ->where('id', $id)
                ->first();
        }
        return view('admins.adverimage.edit')->with("result", $result);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request)
    {
        $id = $request->adverId;
        $adverName = $request->adverName;
        $adverImage = $request->adverImage;
        $time = date('Y-m-d H:i:s', time());
        $data = [];
        if ($adverName) {
            $data['adver_name'] = $adverName;
        }
        if ($adverImage) {
            $data['adver_image'] = $adverImage;
        }
        $result = 0;
        $msg = '修改失败，请重试！';
        if (!empty($data)) {
            $data['updated_at'] = $time;
            $result = $this->model
                ->where('id', $id)
                ->update($data);
            if ($result) {
                $msg = '修改成功！';
            }
        }
        return response()->json(['flag' => $result, 'msg' => $msg]);
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        if ($id) {
            $result = $this->model->where('id', $id);
        }
      return redirect()->route('admin.adverimage.index');
    }

    /*
     * 图片上传
     */
    public function uploadImage(Request $request)
    {
        $year_mouth = date("Ym", time());
        $path = 'storage/upload/images/' . $year_mouth . '/' . date("d", time()) . '/' . auth('admin')->user()->id;
        $upload_file = $request->file('adverImage');

        // 获取文件后缀
        $fileextension = $upload_file->getClientOriginalExtension();
        $filename = date("YmdHis", time()) . '_' . mt_rand(1, 999) . '.' . $fileextension;
        $upload_file->move($path, $filename);
        $uploadFileName = "/".$path . '/' . $filename;

        $upload_file->isValid();
        return response()->json(['images' => $uploadFileName]);
    }
    
}
